#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>

int
main(int argc, char *argv[])
{
	/* parent write and child read from pipe */
	printf("hello world (pid:%d)\n", (int)getpid());
	int pipefd[2];
	char buf;
	pid_t rc;

	if(pipe(pipefd) == -1){
		fprintf(stderr, "pipe failed\n");
		exit(1);
	}

	rc = fork();

	if(rc == -1){
		fprintf(stderr, "fork failed\n");
		exit(1);
	}else if(rc == 0){
		close(pipefd[1]);

		while(read(pipefd[0], &buf, 1) > 0){
			write(STDOUT_FILENO, &buf, 1);
		}

		write(STDOUT_FILENO, "\n", 1);
		close(pipefd[0]);
		_exit(EXIT_SUCCESS);

	}else{
		close(pipefd[0]);
		write(pipefd[1], argv[1], strlen(argv[1]));
		close(pipefd[1]);
		wait(NULL);
		exit(EXIT_SUCCESS);
	}

}
